<?php

/**
 * MyHammer IndexedMemcache
 *
 * LICENSE
 *
 * This source file is subject to the GNU General Public License Version 3
 * that is bundled with this package in the file LICENSE.txt.
 * It is also available through the world-wide-web at this URL:
 * http://www.myhammer.de/opensource/license/gpl.txt
 * If you did not receive a copy of the license and are unable to
 * obtain it through the world-wide-web, please send an email
 * to opensource@myhammer.de so we can send you a copy immediately.
 *
 * @category   MyHammer
 * @package    IndexedMemcache
 * @subpackage Memcache
 * @license    http://www.myhammer.de/opensource/license/gpl.txt GNU General Public License Version 3
 * @version    1.0
 * @author     Steven Berg <berg@myhammer.de>
 */

class cMemCache {

    private static $oMemCache = NULL;
    private $iReads = 0;
    private $iWrites = 0;

    /**
     * write into Memcache
     *
     * @param string $sKey
     * @param mixed $mValue
     * @param integer $iExpire
     * @return boolean
     */
    public static function write($sKey, $mValue, $iExpire = null) {
        return cMemCache::getInstance()->_write($sKey, $mValue, $iExpire);
    }

    /**
     * read from Memcache
     *
     * @param string $sKey
     * @return mixed
     */
    public static function read($sKey) {
        return cMemCache::getInstance()->_read($sKey);
    }

    /**
     * delete from Memcache
     *
     * @param string $sKey
     * @return boolean
     */
    public static function delete($sKey) {
        return cMemCache::getInstance()->_delete($sKey);
    }

    /**
     * increment Id
     *
     * @param string $sKey
     * @return integer
     */
    public static function increment($sKey) {
        return cMemCache::getInstance()->_increment($sKey);
    }

    /**
     * invalidates all existing items
     *
     * @return boolean
     */
    public static function flush() {
        return cMemCache::getInstance()->_flush();
    }

    /**
     * constructor
     */
    private function __construct() {
        $this->connect();
    }

    /**
     * get Object
     *
     * @return cMemCache
     */
    public static function getInstance() {

        if (self::$oMemCache === NULL) {
            self::$oMemCache = new self;
        }
        return self::$oMemCache;
    }

    /**
     * connect to MemCache
     *
     * @return boolean
     */
    private function connect() {
        $this->oMemCache = new Memcache();
        return $this->oMemCache->connect(cConfigMemCache::HOST, cConfigMemCache::PORT);
    }

    /**
     * write
     *
     * @param string $sKey
     * @param mixed $mValue
     * @param integer $iExpire
     * @return boolean
     */
    private function _write($sKey, $mValue, $iExpire) {
        $this->iWrites++;
        return $this->oMemCache->set($sKey, $mValue, cConfigMemCache::MEMCACHE_COMPRESSED, $iExpire);
    }

    /**
     * read
     *
     * @param string $sKey
     * @return mixed
     */
    private function _read($sKey) {
        $this->iReads++;
        return $this->oMemCache->get($sKey);
    }

    /**
     * delete
     *
     * @param string $sKey
     * @return boolean
     */
    private function _delete($sKey) {
        return $this->oMemCache->delete($sKey);
    }

    /**
     * increment
     *
     * @param string $sKey
     * @return integer
     */
    private function _increment($sKey) {
        return $this->oMemCache->increment($sKey);
    }

    /**
     * invalidates all existing items
     *
     * @return boolean
     */
    private function _flush() {
        return $this->oMemCache->flush();
    }
}